﻿@model Yes.Web.Blog.Areas.Admin.Models.Article.IndexView
@{
    ViewBag.Title = "Index";
    Layout = "~/Areas/Admin/Views/Shared/_Layout.cshtml";
}

@section CssJs{
    <script type="text/javascript" charset="utf-8" src="/js/laytpl.js"></script>
    <script type="text/javascript" charset="utf-8" src="/js/laypage.js"></script>
}

<div class="main-wrap">

    <div class="crumb-wrap">
        <div class="crumb-list"><i class="icon-font"></i><a href="@(Url.Action("Index","Home"))">首页</a><span class="crumb-step">&gt;</span><span class="crumb-name">文章管理</span></div>
    </div>
    <div class="search-wrap">
        <div class="search-content">
            <form action="#" method="post">
                <table class="search-tab">
                    <tr>
                        <th width="120">选择分类:</th>
                        <td>
                            <select name="search-sort" id="">
                                <option value="">全部</option>
                                @foreach (var cate in Model.CategoryList)
                                {
                                    <option value="@(cate.Id)">@(cate.Name)</option>
                                }
                            </select>
                        </td>
                        <th width="70">关键字:</th>
                        <td><input class="common-text" placeholder="关键字" name="keywords" value="" id="" type="text"></td>
                        <td><input class="btn btn-primary btn2" name="sub" value="查询" type="submit"></td>
                    </tr>
                </table>
            </form>
        </div>
    </div>
    <div class="result-wrap">
        <form name="myform" id="myform" method="post">
            <div class="result-title">
                <div class="result-list">
                    <a id="batchRecovery" href="javascript:void(0)"><i class="icon-font"></i>恢复</a>
                    <a id="batchDel" href="javascript:void(0)"><i class="icon-font"></i>彻底删除</a>
                </div>
            </div>
            <div class="result-content">
                <table class="result-tab" width="100%">
                    <tr>
                        <th class="tc" width="5%"><input class="allChoose" name="checkAll" id="checkAll" type="checkbox"></th>
                        <th>ID</th>
                        <th>分类</th>
                        <th>标题</th>
                        <th>点击</th>
                        <th>评论</th>
                        <th>发布人</th>
                        <th>发布时间</th>
                    </tr>
                    <tbody id="tbody"></tbody>
                </table>
                <div class="list-page" id="page"></div>
            </div>
        </form>
    </div>
</div>

<script id="list" type="text/html">
    {{# for(var i = 0, len = d.length; i < len; i++){ }}
    <tr>
        <td class="tc"><input name="checkname" value="{{ d[i].id }}" type="checkbox"></td>
        <td>
            <input name="Id" value="{{ d[i].id }}" type="hidden">
            {{ d[i].id }}
        </td>
        <td>{{ d[i].categoryName }}</td>
        <td title="{{ d[i].title }}">
            <a href="/Admin/Article/Add/{{ d[i].id }}" title="{{ d[i].title }}">{{ d[i].title }}</a>
        </td>
        <td>{{ d[i].readCount }}</td>
        <td>{{ d[i].commentCount }}</td>
        <td>{{ d[i].userName }}</td>
        <td>{{ d[i].publishDate }}</td>
    </tr>
    {{# } }}
    {{# if(d.length===0){ }}
    <tr>
        <td colspan="10">回收站没有任何文章</td>
    </tr>
    {{# } }}
</script>
<script>
    var pageIndex = 1;
    $(function() {
        GetArticlePager();

        $("#checkAll").click(
            function() {
                if (this.checked) {
                    $("input[name='checkname']").prop('checked', true);
                } else {
                    $("input[name='checkname']").prop('checked', false);
                }
            }
        );

        $("#batchRecovery").click(
            function() {
                var ids = $("input[name='checkname']:checked").map(function(index, elem) { return $(elem).val(); })
                    .get();
                if (ids) {
                    $.ajax({
                        type: "POST",
                        url: "@(Url.Action("Recovery", "Article"))",
                        data: { ids: ids },
                        success: function(result) {
                            if (result.status) {
                                GetArticlePager();
                            } else {
                                var error = result.errorMessage;
                                if (result.data) {
                                    error += "<br>" + result.data;
                                }
                                layer.alert(error, { icon: 5 });
                            }
                        }
                    });
                }
            }
        );

        $("#batchDel").click(
            function() {
                var ids = $("input[name='checkname']:checked").map(function(index, elem) { return $(elem).val(); })
                    .get();
                if (ids) {
                    $.ajax({
                        type: "POST",
                        url: "@(Url.Action("Delete", "Article"))",
                        data: { ids: ids },
                        success: function(result) {
                            if (result.status) {
                                $("input[name='checkname']:checked").each(function(index, elem) {
                                    $(elem).parents("tr").remove();
                                });
                            } else {
                                var error = result.errorMessage;
                                if (result.data) {
                                    error += "<br>" + result.data;
                                }
                                layer.alert(error, { icon: 5 });
                            }
                        }
                    });
                }
            }
        );
    });


    function GetArticlePager() {
        $.ajax({
            type: "POST",
            url: "@(Url.Action("GetPageInfo", "Article"))",
            data: { IsRecycle: true },
            success: function(result) {
                if (result.status) {
                    laytpl($("#list").html()).render(result.data.list,
                        function(html) {
                            $("#tbody").html(html);
                        });
                    laypage({
                        cont: 'page',
                        pages: result.data.pageCount,
                        curr: pageIndex || 1,
                        jump: function (obj, first) {
                            if (!first) {
                                pageIndex = obj.curr;
                                GetArticlePager();
                            }
                        }
                    });
                } else {
                    var error = result.errorMessage;
                    if (result.data) {
                        error += "<br>" + result.data;
                    }
                    layer.alert(error, { icon: 5 });
                }
            }
        });
    }

</script>
